package com.hxd.otc.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.JsonSyntaxException;
import com.hxd.otc.base.BaseApplication;

import java.lang.reflect.Type;
import java.util.Set;

/**
 * Created by zyzd on 17/11/15.
 * <p>
 * 建议：
 * USER_INFO： 该实例用于缓存用户信息
 * OTHER：该实例用于其他信息
 * 可以根据需求增加实例
 */

public enum SpUtil {

    CACHE("cache"), PERMANENT("permanent");
    private SharedPreferences mSp;
    private SharedPreferences.Editor mEditor;

    @SuppressLint("CommitPrefEdits")
    SpUtil(String fileName) {
        mSp = getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        mEditor = mSp.edit();
    }

    public void putBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }

    public void putInt(String key, int value) {
        mEditor.putInt(key, value).commit();
    }

    public int getInt(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }

    public void putLong(String key, long value) {
        mEditor.putLong(key, value).commit();
    }

    public long getLong(String key, long defValue) {
        return mSp.getLong(key, defValue);
    }

    public void putFloat(String key, float value) {
        mEditor.putFloat(key, value).commit();
    }

    public float getFloat(String key, float defValue) {
        return mSp.getFloat(key, defValue);
    }

    public void putString(String key, String value) {
        mEditor.putString(key, value).commit();
    }

    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }

    public void putStringSet(String key, Set<String> set) {
        mEditor.putStringSet(key, set).commit();
    }

    public Set<String> getStringSet(String key, Set<String> defSet) {
        return mSp.getStringSet(key, defSet);
    }

    public void putObject(String key, Object object) {
        putString(key, object == null ? "" : GsonUtil.toJson(object));
    }

    /**
     * 该方法仅用于保存用户信息
     *
     * @param key
     * @param object
     * @param encryption
     */
    public void putObject(String key, Object object, boolean encryption) {
        if (encryption) {// 用户信息是否加密
            putString(key, object == null ? "" : EncryptUtils.convertMD5(GsonUtil.toJson(object)));
        } else {
            putString(key, object == null ? "" : GsonUtil.toJson(object));
        }
    }


    public <T> T getObject(String key, Type typeOfT) {
        try {
            return GsonUtil.fromJson(getString(key, ""), typeOfT);
        } catch (JsonSyntaxException e) {
            return null;
        }
    }

    /**
     * 该方法仅用于保存用户信息
     *
     * @param key
     * @param typeOfT
     * @param encryption
     * @param <T>
     * @return
     */
    public <T> T getObject(String key, Type typeOfT, boolean encryption) {
        try {
            if (encryption) {//如果已经加密，则解密
                String userInfo = getString(key, "");
                return GsonUtil.fromJson(EncryptUtils.convertMD5(userInfo), typeOfT);
            } else {//如果还没有加密，则不解密
                return GsonUtil.fromJson(getString(key, ""), typeOfT);
            }
        } catch (JsonSyntaxException e) {
            LogUtils.e("====JsonSyntaxException===" + e.getMessage());
            return null;
        }
    }

    public void remove(String key) {
        if (mSp.contains(key))
            mEditor.remove(key).commit();
    }

    public void clear() {
        mEditor.clear().commit();
    }

    private Context getContext() {
        return BaseApplication.mApp;
    }
}